﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using App1AppHandler;
using System.Drawing;
using Reborn_WorldServer;

namespace Reborn_Server.App1AppHandler
{
    public class Spell_ResistFire : Buff
    {
        private int ResistAdded = 25;
        private int PrismaticResistAdded = 10;

        public Spell_ResistFire()
        {
            minutesLeft = 10;
        }

        private CreatureBase choosenTarget;
        public override bool hasCustomTarget(SpellIcon sIcon)
        {
            return true;
        }
        public override bool hasValidTarget(SpellIcon sIcon, Creature creature, List<Point> hiddenList)
        {
            //this.hiddenList = hiddenList;

            if (creature.buffManager.GetBuffByStackType(sIcon.StackType) == null)
            {
                choosenTarget = creature;
                return true;
            }

            List<CreatureBase> targetCreatures = new List<CreatureBase>();
            foreach (MapItem mapitem in creature.currentMapItem.mapItemsWithin4)
            {
                if (CreatureAI.isHidden(mapitem.MapX, mapitem.MapY, hiddenList) == false)
                {
                    foreach (CreatureBase creatureBase in mapitem.CreaturesBases)
                    {
                        if (creatureBase.Faction == creature.Faction && creatureBase.buffManager.GetBuffByStackType(sIcon.StackType) == null)
                            targetCreatures.Add(creatureBase);
                    }
                }
            }

            if (targetCreatures.Count > 0)
            {
                int indexToChoose = Managers.GameHandler.random.Next(0, targetCreatures.Count);
                choosenTarget = targetCreatures[indexToChoose];
            }

            if (choosenTarget == null)
                return false;
            else
                return true;
        }

        override public void Resume(CreatureBase targetCreatureBase, SpellIcon _spellIcon, int timeRemaining, int castLevel, int castCharacterID, int castInt, int castWis, int optionalValue, string xmlData)
        {
            minutesLeft = timeRemaining;
            base.Resume(targetCreatureBase, _spellIcon, timeRemaining, castLevel, castCharacterID, castInt, castWis, optionalValue, xmlData);

            if (_spellIcon.CastType == "Target")
            {
                ResistAdded = SkillManager.AdjustForCombat(castLevel / 2, ResistAdded, CastingCreatureWis / 3);
                PrismaticResistAdded = SkillManager.AdjustForCombat(castLevel / 2, PrismaticResistAdded, CastingCreatureWis / 5);
            }
            else if (_spellIcon.CastType == "Self")
            {
                ResistAdded = SkillManager.AdjustForCombat(castLevel, ResistAdded, CastingCreatureWis / 3);
                PrismaticResistAdded = SkillManager.AdjustForCombat(castLevel, PrismaticResistAdded, CastingCreatureWis / 5);
            }

            if (spellIcon.Skill == "Fire")
                TargetCreatureBase.ResistFire = TargetCreatureBase.ResistFire + ResistAdded;
            else if (spellIcon.Skill == "Ice")
                TargetCreatureBase.ResistIce = TargetCreatureBase.ResistIce + ResistAdded;
            else if (spellIcon.Skill == "Holy")
                TargetCreatureBase.ResistHoly = TargetCreatureBase.ResistHoly + ResistAdded;
            else if (spellIcon.Skill == "Unholy")
                TargetCreatureBase.ResistUnholy = TargetCreatureBase.ResistUnholy + ResistAdded;
            else if (spellIcon.Skill == "Mentality")
                TargetCreatureBase.ResistMagic = TargetCreatureBase.ResistMagic + ResistAdded;
            else if (spellIcon.Skill == "Shadow")
                TargetCreatureBase.ResistPhysical = TargetCreatureBase.ResistPhysical + ResistAdded;
            else if (spellIcon.Skill == "Resilience")
            {
                TargetCreatureBase.ResistFire = TargetCreatureBase.ResistFire + PrismaticResistAdded;
                TargetCreatureBase.ResistIce = TargetCreatureBase.ResistIce + PrismaticResistAdded;
                TargetCreatureBase.ResistHoly = TargetCreatureBase.ResistHoly + PrismaticResistAdded;
                TargetCreatureBase.ResistUnholy = TargetCreatureBase.ResistUnholy + PrismaticResistAdded;
                TargetCreatureBase.ResistMagic = TargetCreatureBase.ResistMagic + PrismaticResistAdded;
                TargetCreatureBase.ResistPhysical = TargetCreatureBase.ResistPhysical + PrismaticResistAdded;
            }
            TargetCreatureBase.Update_Character();

            //Update screen to reveal hidden creatures
            if (spellIcon.Skill == "Resilience" || spellIcon.Skill == "Shadow")
            {
                LockItem lockitem = TargetCreatureBase.GameHandler.GetLockItem(TargetCreatureBase.MapID, TargetCreatureBase.Instance);
                lock (lockitem.LockObj)
                {
                    Map CurrentMap = TargetCreatureBase.GameHandler.GetMap(TargetCreatureBase.MapID, TargetCreatureBase.Instance);

                    List<MapItem> MapItemsToUpdate = new List<MapItem>();
                    foreach (MapItem mapitem in CurrentMap.MapItems)
                    {
                        //Get nearby mapitems that have an invisible creature
                        if (Math.Abs(mapitem.MapX - TargetCreatureBase.MapX) <= 4 && Math.Abs(mapitem.MapY - TargetCreatureBase.MapY) <= 4)
                        {
                            foreach (CreatureBase creaturebase in mapitem.CreaturesBases)
                            {
                                if (creaturebase.buffManager.GetBuffByStackType("Hide") != null)
                                {
                                    MapItemsToUpdate.Add(mapitem);
                                    break;
                                }
                            }
                        }
                    }
                    if (TargetCreatureBase is Character && MapItemsToUpdate.Count > 0)
                    {
                        Character targetCharacter = (Character)TargetCreatureBase;
                        targetCharacter.AddMapItems(MapItemsToUpdate, false);
                    }
                }
            }

            spellTimer.Start();
        }
        public void Cast(CreatureBase myCreatureBase, int CastLevel, long CreatureGUID)
        {
            //If its a creature custom cast, replace the targets ID
            if (choosenTarget != null)
                CreatureGUID = choosenTarget.CreatureID;

            List<MapItem> MapItemsWithin12;
            if (base.InitializeCast(myCreatureBase, CastLevel, CreatureGUID, true, out MapItemsWithin12) == false)
                return;

            if (spellIcon.CastType == "Target")
            {
                ResistAdded = SkillManager.AdjustForCombat(CastLevel / 2, ResistAdded, CastingCreatureWis / 3);
                PrismaticResistAdded = SkillManager.AdjustForCombat(CastLevel / 2, PrismaticResistAdded, CastingCreatureWis / 5);
            }
            else if (spellIcon.CastType == "Self")
            {
                ResistAdded = SkillManager.AdjustForCombat(CastLevel, ResistAdded, CastingCreatureWis / 3);
                PrismaticResistAdded = SkillManager.AdjustForCombat(CastLevel, PrismaticResistAdded, CastingCreatureWis / 5);
            }

            minutesLeft = System.Convert.ToInt32(Math.Floor(CastingCreatureWis / 2.00));

            if (spellIcon.Skill == "Fire")
                TargetCreatureBase.ResistFire = TargetCreatureBase.ResistFire + ResistAdded;
            else if (spellIcon.Skill == "Ice")
                TargetCreatureBase.ResistIce = TargetCreatureBase.ResistIce + ResistAdded;
            else if (spellIcon.Skill == "Holy")
                TargetCreatureBase.ResistHoly = TargetCreatureBase.ResistHoly + ResistAdded;
            else if (spellIcon.Skill == "Unholy")
                TargetCreatureBase.ResistUnholy = TargetCreatureBase.ResistUnholy + ResistAdded;
            else if (spellIcon.Skill == "Mentality")
                TargetCreatureBase.ResistMagic = TargetCreatureBase.ResistMagic + ResistAdded;
            else if (spellIcon.Skill == "Shadow")
                TargetCreatureBase.ResistPhysical = TargetCreatureBase.ResistPhysical + ResistAdded;
            else if (spellIcon.Skill == "Resilience")
            {
                TargetCreatureBase.ResistFire = TargetCreatureBase.ResistFire + PrismaticResistAdded;
                TargetCreatureBase.ResistIce = TargetCreatureBase.ResistIce + PrismaticResistAdded;
                TargetCreatureBase.ResistHoly = TargetCreatureBase.ResistHoly + PrismaticResistAdded;
                TargetCreatureBase.ResistUnholy = TargetCreatureBase.ResistUnholy + PrismaticResistAdded;
                TargetCreatureBase.ResistMagic = TargetCreatureBase.ResistMagic + PrismaticResistAdded;
                TargetCreatureBase.ResistPhysical = TargetCreatureBase.ResistPhysical + ResistAdded;
            }

            TargetCreatureBase.buffManager.UpdateBuffs();
            TargetCreatureBase.Update_Character();

            List<MapItem> MapItemsToUpdate = new List<MapItem>();
            foreach (MapItem mapitem in MapItemsWithin12)
            {
                if (Math.Abs(mapitem.MapX - TargetCreatureBase.MapX) <= 8 && Math.Abs(mapitem.MapY - TargetCreatureBase.MapY) <= 8)
                {
                    foreach (CreatureBase creaturebase in mapitem.CreaturesBases)
                    {
                        if (creaturebase is Character)
                        {
                            Character character = (Character)creaturebase;
                            character.PlaySound("BuffCast", TargetCreatureBase.MapX, TargetCreatureBase.MapY);
                        }
                    }
                }
                //Get nearby mapitems that have an invisible creature
                if (Math.Abs(mapitem.MapX - TargetCreatureBase.MapX) <= 4 && Math.Abs(mapitem.MapY - TargetCreatureBase.MapY) <= 4)
                {
                    foreach (CreatureBase creaturebase in mapitem.CreaturesBases)
                    {
                        if (creaturebase.buffManager.GetBuffByStackType("Hide") != null)
                        {
                            MapItemsToUpdate.Add(mapitem);
                            break;
                        }
                    }
                }
            }
            if (MapItemsToUpdate.Count > 0)
            {
                if (TargetCreatureBase is Character)
                {
                    Character targetCharacter = (Character)TargetCreatureBase;
                    targetCharacter.AddMapItems(MapItemsToUpdate, false);
                }
            }


            spellTimer.Start();
        }
        public override void RemoveSpell(bool updateBuffs, bool sendMessage)
        {
            base.RemoveSpell(updateBuffs, sendMessage);

            if (spellIcon.Skill == "Fire")
                TargetCreatureBase.ResistFire = TargetCreatureBase.ResistFire - ResistAdded;
            else if (spellIcon.Skill == "Ice")
                TargetCreatureBase.ResistIce = TargetCreatureBase.ResistIce - ResistAdded;
            else if (spellIcon.Skill == "Holy")
                TargetCreatureBase.ResistHoly = TargetCreatureBase.ResistHoly - ResistAdded;
            else if (spellIcon.Skill == "Unholy")
                TargetCreatureBase.ResistUnholy = TargetCreatureBase.ResistUnholy - ResistAdded;
            else if (spellIcon.Skill == "Mentality")
                TargetCreatureBase.ResistMagic = TargetCreatureBase.ResistMagic - ResistAdded;
            else if (spellIcon.Skill == "Shadow")
                TargetCreatureBase.ResistPhysical = TargetCreatureBase.ResistPhysical - ResistAdded;
            else if (spellIcon.Skill == "Resilience")
            {
                TargetCreatureBase.ResistFire = TargetCreatureBase.ResistFire - PrismaticResistAdded;
                TargetCreatureBase.ResistIce = TargetCreatureBase.ResistIce - PrismaticResistAdded;
                TargetCreatureBase.ResistHoly = TargetCreatureBase.ResistHoly - PrismaticResistAdded;
                TargetCreatureBase.ResistUnholy = TargetCreatureBase.ResistUnholy - PrismaticResistAdded;
                TargetCreatureBase.ResistMagic = TargetCreatureBase.ResistMagic - PrismaticResistAdded;
                TargetCreatureBase.ResistPhysical = TargetCreatureBase.ResistPhysical - ResistAdded;
            }

            TargetCreatureBase.Update_Character();

            //Update screen to reveal hidden creatures
            if (spellIcon.Skill == "Resilience" || spellIcon.Skill == "Shadow")
            {
                LockItem lockitem = TargetCreatureBase.GameHandler.GetLockItem(TargetCreatureBase.MapID, TargetCreatureBase.Instance);
                lock (lockitem.LockObj)
                {
                    Map CurrentMap = TargetCreatureBase.GameHandler.GetMap(TargetCreatureBase.MapID, TargetCreatureBase.Instance);

                    List<MapItem> MapItemsToUpdate = new List<MapItem>();
                    foreach (MapItem mapitem in CurrentMap.MapItems)
                    {
                        //Get nearby mapitems that have an invisible creature
                        if (Math.Abs(mapitem.MapX - TargetCreatureBase.MapX) <= 4 && Math.Abs(mapitem.MapY - TargetCreatureBase.MapY) <= 4)
                        {
                            foreach (CreatureBase creaturebase in mapitem.CreaturesBases)
                            {
                                if (creaturebase.buffManager.GetBuffByStackType("Hide") != null)
                                {
                                    MapItemsToUpdate.Add(mapitem);
                                    break;
                                }
                            }
                        }
                    }
                    if (TargetCreatureBase is Character && MapItemsToUpdate.Count > 0)
                    {
                        Character targetCharacter = (Character)TargetCreatureBase;
                        targetCharacter.AddMapItems(MapItemsToUpdate, false);
                    }
                }
            }
        }
    }
}
